import os
from enum import Enum, unique


class ConfigSections:
    GENERAL = 'general'
    COMPILER = 'compiler'


class ConfigOptions:
    ACTION = 'action'
    MODE = 'mode'
    BENCHMARKS = "benchmarks"
    SUITES = 'suites'
    TEST_TARGET = 'test_target'
    ITERATIONS = 'iterations'
    COPIES = 'copies'
    TASKSET = 'taskset'
    REPORT = 'report'
    OUTPUT_DIR = 'output_dir'
    SKIP_FAIL = 'skip_fail'
    SKIP_VERIFY = 'skip_verify'
    AUTO_UPLOAD = 'auto_upload'
    DEBUG = 'debug'
    MANUFACTURER = 'manufacturer'
    PRODUCT_MODEL = 'product_model'
    CPU_MODEL = 'cpu_model'
    NETCARDS = 'netcards'
    RAID = 'raid'
    DISPLAY_CARDS = 'display_cards'
    HARDWARE_OTHERS = 'hardware_others'
    OS = 'os'
    SOFTWARE_OTHERS = 'software_others'
    TESTER_NAME = 'tester_name'
    TEST_DATE = 'test_date'
    LIST_WORKLOADS = 'list_workloads'
    LIST_SUITES = 'list_suties'
    VERSION = 'VERSION'
    HELP = 'HELP'
    CONFIG = 'CONFIG'
    BIND_COPIES = 'bind_copies'
    BIND_CORES = 'bind_cores'

    # COMPILER OPTIONS
    BASE_DIR = 'base_dir'
    LD_LIBRARY_PATH = 'LD_LIBRARY_PATH'
    LD_FLAGS = 'LD_FLAGS'
    CC = 'CC'
    CC_VERSION = 'CC_VERSION'
    CFLAGS = 'CFLAGS'

    CLD = 'CLD'
    CLD_FLAGS = 'CLD_FLAGS'

    CXX = 'CXX'
    CXX_VERSION = 'CXX_VERSION'
    CXXFLAGS = 'CXXFLAGS'

    CXXLD = 'CXXLD'
    CXXLD_FLAGS = 'CXXLD_FLAGS'

    JAVA_OPTIONS = 'java_options'
    LIBS = 'LIBS'
    AR = 'AR'
    RANLIB = 'RANLIB'


class BenchAction:
    STANDARD = 'standard'
    BUILD = 'build'
    RUN = 'run'
    CLEAN = 'clean'

class BenchMode:
    SERVER = 'Server'
    DESKTOP = 'Desktop'
    LAPTOP = 'Laptop'
    TSSERVER = 'TSServer'
    TSDESKTOP = 'TSDesktop'
    DEFAULT = 'Default'

@unique
class StatusCode(Enum):
    SUCCESS = 0
    OPTION_PARSE_FAIL = 1
    OPTION_CHECK_FAIL = 2
    OPTION_MERGE_FAIL = 3
    FILE_READ_FAIL = 4
    INVALID_TYPE = 5
    LOAD_SUITE_FAILED = 6
    UNKNOWN_TEST_TARGET = 7
    RUN_FAIL = 8
    BUILD_FAIL = 9
    VALIDATE_FAIL = 10
    GET_RAW_SCORE_FAIL = 11
    BUILD_EXTEND_TOOL_FAIL = 12
    CLEAN_FAIL = 13
    SETUP_INSTANCE_FAIL = 14
    TEAR_DOWN_INSTANCE_FAIL = 15
    VALIDATE_DEPENDENCE_FAIL = 16


class ResultType:
    Suite = 0
    Workload = 1


class ReportType:
    PDF = 'pdf'
    JSON = 'json'
    TXT = 'txt'
    CSV = 'csv'
    ALL = 'all'


class MeasureUnit:
    Default = ''              # None
    SCORE = 'score'           # default score
    FPS = 'fps'               # Frame Per Second                                             | 每秒帧数
    KBps = 'KB/s'             # 1KB=1000Byte                                                 | 每秒的千字节数
    KiBps = 'KiB/s'           # 1KiB=1024Byte                                                | 每秒的千字节数
    MBps = 'MB/s'             # 1MB=1000KB                                                   | 每秒的兆字节数
    MiBps = 'MiB/s'           # 1MiB=1024KiB                                                 | 每秒的兆字节数
    GBps = 'GB/s'             # 1GB=1000MB                                                   | 每秒的兆字节数
    GiBps = 'GiB/s'           # 1GiB=1024MiB                                                 | 每秒的兆字节数
    LoopPerDay = 'lpd'        # Loops Per Day                                                | 每天运行次数
    LoopPerHour = 'lph'       # Loops Per Hour                                               | 每小时运行次数
    LoopPerMin = 'lpm'        # Loops Per Minute                                             | 每分钟运行次数
    LoopPerMonth = 'lpmo'     # Loops Per Month                                              | 每月运行次数
    LoopPerSec = 'lps'        # Loops Per Second                                             | 每秒运行次数
    MFLOPS = 'Mflops'         # Million Floating Point Operations Per Second                 | 每秒的兆浮点运算数
    GFLOPS = 'Gflops'         # Giga Floating Point Operations Per Second                    | 每秒的兆浮点运算数
    MWIPS = 'MWIPS'           # Million Whetstones Instructions Per Second                   | 每秒的兆指令数
    BytePerSec = 'Byte/s'     # Bytes Per Second                                             | 每秒字节数
    RecordPerSec = 'recps'    # Records Per Second. (from ebizzy)                            | 每秒记录数
    IOPS = 'iops'             # IO (the geometric mean of read, write, and other) Per Second.| 每秒的IO数
    RPS = 'rps'               # Request Per Second.                                          | 每秒请求数
    TPS = 'TPS'               # Transactions Per Second.                                     | 每秒请求数
    MBPerMins = 'MBpms'       # MB Per Minute                                                | 每分钟兆字节数
    LogLoopPerMin = 'llpm'    # Log Loops Per Minute                                         | log每分钟运行次数
    LogLoopPerSec = 'llps'    # Log Loops Per Second                                         | log每秒运行次数
    logLoopPerHour = 'llph'   # Loops Per Hour                                               | 每小时运行次数
    LogLoopPerMins = 'llpms'  # Log Loops Per Minute                                         | log每分钟运行次数
    Second = 'sec'            # Second                                                       | 秒数
    MS = 'ms'                 # millisecond.                                                 | 毫秒
    MIN = 'min'               # minute.                                                      | 分钟


class GlobalVar:
    LOG_NAME = None
    VERSION = None
    SKIP_FAIL = 0
    URL = ""
    TIMEOUT = 3


class GlobalEnv:
    TOOL_NAME = "GCBS"
    GCBS_ROOT_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
    GCBS_WORKLOAD_DIR = os.path.join(GCBS_ROOT_DIR, "workload")
    GCBS_DEFAULT_OUTPUT_DIR = os.path.join(GCBS_ROOT_DIR, "output_dir")
    GCBS_FONTS_DIR = os.path.join(GCBS_ROOT_DIR, "fonts")
    GCBS_TMP_DIR = os.path.join(GCBS_ROOT_DIR, "tmp")
    GCBS_UTILS_DIR = os.path.join(GCBS_ROOT_DIR, "utils")


class MachineOptions:
    LANG = "LANG"
    NAME = "name"
    CAPACITY = "capacity"
    MODEL = "model"

    # hardware key
    BIOS_VERSION = "bios_version"
    ARCH = "arch"
    CPU_NOMINAL_MHZ = "cpu_nominal_mhz"
    CPU_MAX_MHZ = "cpu_max_mhz"
    CPU_BOGOMIPS = "cpu_bogomips"
    CPU_CORES = "cpu_cores"
    CPU_LOGIC_CORES = "cpu_logic_cores"
    SOCKETS = "sockets"
    L1D_CACHE = "l1d_cache"
    L1I_CACHE = "l1i_cache"
    L2_CACHE = "l2_cache"
    L3_CACHE = "l3_cache"
    NUMA_TOPOLOGY = "numa_topology"
    MEMORY = "memory"
    MEMORY_TOTAL = "memory_total"
    MEMORY_COUNT = "memory_count"
    MEMORY_CAPACITY = "memory_capacity"
    MEMORY_MANU = "memory_manu"
    MEMORY_TYPE = "memory_type"
    MEMORY_SPEED = "memory_speed"
    STORAGE = "storage"
    OS_DISK = "os_disk"
    DISK = "disk"

    # software key
    SYSTEM = "system"
    KERNEL = "kernel"
    LANGUAGE = "language"
    CMDLINE = "cmdline"
    PAGE_SIZE = "page_size"
    HUGE_PAGE_SIZE = "huge_page_size"
    THP = "transparent_huge_pages"
    RUN_LEVEL = "run_level"
    BASH = "bash"
    COMPILER = "compiler"
    GCC = "gcc"
    GXX = "gxx"
    BINUTILS = "binutils"
    CC = "cc"
    CXX = "cxx"
    PYTHON2 = "python2"
    PYTHON3 = "python3"
    JAVA = "java"
    C_OPTIONS = "c_options"
    CXX_OPTIONS = "cxx_options"
    JAVA_OPTIONS = "java_options"
    GLIBC = "glibc"
    FILE_SYSTEM_PARAM = "file_system_param"
    FILE_SYSTEM = "file_system"
    MOUNT_PARAM = "mount_param"
    IO_SCHEDULER = "io_scheduler"


class ValidateOptions:
    IGNORE_DIRS = ("__pycache__", )
    IGNORE_ROOT_DIRS = ("buildtools", "buildtools_install", "config", "docs", "fonts", "tmp",
                        "unittest", "output_dir", "__pycache__", ".git")
    IGNORE_WD_DIRS = ("build", "run", "log", "bin", "__pycache__",)
    HASH_FILE = "gcbs_hash_file.dat"
